/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jsalon.repo;

import jamjun.infra.common.DBHelper;
import jamjun.infra.common.SqlParameter;
import jamjun.infra.lang.JJString;
import jamjun.infra.model.Paging;
import jamjun.infra.model.UserSession;
import jamjun.infra.model.UserSessionFilter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jsalon.enumtype.Gender;
import jsalon.model.Customer;
import jsalon.model.CustomerFilter;

/**
 *
 * @author Java
 */
public class UserSessionRepo implements jsalon.repo.irepo.IUserSessionRepo {

    @Override
    public UserSession insert(UserSession model) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<UserSession> getList(Paging paging, UserSessionFilter modelFilter) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    public UserSession getFirst(String sessionID) throws Exception{
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try { 
            conn = DBHelper.getConnection(jamjun.infra.model.DBConnectionName.jsalon.Name());
            //<editor-fold desc="model filter.">
            String sqlFilterCMD = "";
            String opSQL = " and ";
            List<SqlParameter> SqlParameterList = new ArrayList<>();
            if(!JJString.IsNullOrEmty(sessionID)){
                sqlFilterCMD += (!JJString.IsNullOrEmty(sqlFilterCMD)) ? opSQL : "";
                sqlFilterCMD += " sessionID=@sessionID ";
                SqlParameter sqlParameter = new  SqlParameter();
                sqlParameter.SetString("@sessionID",sessionID);
                SqlParameterList.add(sqlParameter);
            }else{
                throw new Exception("Not have sessionID.");
            }
            sqlFilterCMD = (!JJString.IsNullOrEmty(sqlFilterCMD)) ? " where " + sqlFilterCMD : "";
            //</editor-fold>
            
            String sql = "SELECT * FROM user_session " + sqlFilterCMD;
            pstmt = DBHelper.setPreparedStatement(conn,sql,SqlParameterList);
            rs = pstmt.executeQuery();
            UserSession model = new UserSession();
            while(rs.next()){
               model.domainID = rs.getString("domainID");
               model.sessionID = rs.getString("sessionID");
               model.strTimeZone = rs.getString("timeZone");
               model.userID = rs.getString("userID");
               model.strLocale  = rs.getString("locale");
               model.createDate = rs.getDate("createDate");
            }
            rs.close();
            return model;
        } catch (SQLException ex) {
            Logger.getLogger(UserSessionRepo.class.getName()).log(Level.SEVERE, null, ex);
            throw new Exception(ex.getMessage());
        } finally {
            if(rs != null){rs.close(); }
            if(conn != null){conn.close();}
        } 
    }
}
